package com.snap.vseries.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.snap.vseries.AppConfig;
import com.snap.vseries.R;
import com.snap.vseries.base.BaseFragment;
import com.snap.vseries.bean.ExpInfo;
import com.snap.vseries.manager.FileManager;
import com.snap.vseries.model.DBManager;
import com.snap.vseries.ui.widget.DialogView;
import com.snap.vseries.ui.widget.TextButton;
import com.snap.vseries.utils.AutoUtil;
import com.snap.vseries.utils.ToastUtils;

import org.litepal.crud.callback.UpdateOrDeleteCallback;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 实验结果：报告
 */
@SuppressLint("ValidFragment")
public class DetailInfoFragment extends BaseFragment {

    private static final String TAG = "DetailInfoFragment";

    @BindView(R.id.exp_name_tv)
    TextView expNameTv;
    @BindView(R.id.sample_name_tv)
    TextView sampleNameTv;
    @BindView(R.id.programs_tv)
    TextView programsTv;
    @BindView(R.id.start_time_tv)
    TextView startTimeTv;
    @BindView(R.id.end_time_tv)
    TextView endTimeTv;
    @BindView(R.id.container)
    FrameLayout container;
    @BindView(R.id.delete_exp_btn)
    TextButton deleteExpBtn;
    private ExpInfo experimentInfo;
    private ResultTableListFragment listFragment;

    public DetailInfoFragment(ExpInfo experimentInfo) {
        this.experimentInfo = experimentInfo;
    }

    @Override
    protected int layoutResId() {
        return R.layout.fragment_info;
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        initProperty();
        initProgram();
        if (AppConfig.Dynamic.custom_setting) {
            deleteExpBtn.setVisibility(View.VISIBLE);
        }
    }

    private void initProperty() {
        if (experimentInfo != null) {
            expNameTv.setText(experimentInfo.getName());
            startTimeTv.setText(AutoUtil.getHour(experimentInfo.getStartTime()));
            endTimeTv.setText(AutoUtil.getHour(experimentInfo.getEndTime()));
            sampleNameTv.setText((experimentInfo.getDeviceTag()==null?"N/A":experimentInfo.getDeviceTag())+" - "+(experimentInfo.getSampleName() == null ? "N/A" : experimentInfo.getSampleName()));
            programsTv.setText(experimentInfo.getTestProject());
        }
    }

    private void initProgram() {
        listFragment = new ResultTableListFragment();
        listFragment.setExperimentInfo(experimentInfo);
        getChildFragmentManager().beginTransaction().add(R.id.list_container, listFragment).commitAllowingStateLoss();
        container.setVisibility(View.VISIBLE);
        ProgramFragment programFragment = new ProgramFragment(experimentInfo);
        getChildFragmentManager().beginTransaction().add(R.id.container, programFragment).commitAllowingStateLoss();
    }

    @OnClick({R.id.delete_exp_btn})
    public void click(View view) {
        switch (view.getId()) {
            case R.id.delete_exp_btn:
                final DialogView dialogView = new DialogView(getContext());
                dialogView.setTitle("删除实验");
                dialogView.setTextContent("删除后不可恢复，是否确认删除？");
                dialogView.setOnClickListener("仍然删除", new DialogView.OnSureListener() {
                    @Override
                    public void onSureClick(String content) {
                        deleteExp();
                    }
                }).show();
            default:
        }
    }

    private void deleteExp() {
        DBManager.get().deleteExp(new UpdateOrDeleteCallback() {
            @Override
            public void onFinish(int rowsAffected) {
                if (rowsAffected > 0) {
                    boolean success = FileManager.getInstance().removeExpData(experimentInfo.getName());
                    if (success) {
                        ToastUtils.get().showText("删除实验成功");
                    } else {
                        ToastUtils.get().showText("删除实验失败");
                    }
                    if (getActivity() != null) {
                        getActivity().finish();
                    }
                }
            }
        }, experimentInfo.getName());
    }

    public void changeAnalyzeType() {
        if (listFragment != null) {
            listFragment.changeAnalyzeType();
        }
    }
}